package com.fish.yssb.ui.page.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fish.yssb.Consts;
import com.fish.yssb.R;
import com.fish.yssb.app.BaseApp;
import com.fish.yssb.data.model.LoginUser;
import com.fish.yssb.data.model.TopRK;
import com.fish.yssb.ui.page.activity.constPara.ConstParaActivity;
import com.fish.yssb.ui.page.activity.realTime.RealTimeListActivity;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by zhuguohui on 2016/11/8.
 */

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.MyViewHolder> {

//    private static List<String> data = new ArrayList<>();

    private Random random=new Random();
    private Context context ;
    private  static List<TopRK> data = new ArrayList<>();
    public AppAdapter(Context context, List<TopRK> topAppData){
        this.data = topAppData;
        this.context = context;
    }



    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.top_app_layout_item, parent, false);
        return new MyViewHolder(view);
    }



    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final TopRK app = data.get(position);

       final LoginUser user = BaseApp.getInstance().user;

        holder.tv_title.setText(app.getTitle());
        if(Consts.toprk1.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_1);
        }else  if(Consts.toprk2.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_2);
        }else  if(Consts.toprk3.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_3);
        }else  if(Consts.toprk4.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_4);
        }else  if(Consts.toprk5.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_5);
        }else  if(Consts.toprk6.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_6);
        }else  if(Consts.toprk7.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_7);
        }else  if(Consts.toprk8.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_8);
        }else  if(Consts.toprk9.equalsIgnoreCase(app.getTitle()))
        {

            if("".equalsIgnoreCase(user.getUserName()))
            {
                holder.im_icon.setImageResource(R.drawable.top_rk_9_u);
            }else{
                holder.im_icon.setImageResource(R.drawable.top_rk_9);
            }

        }else  if(Consts.toprk10.equalsIgnoreCase(app.getTitle()))
        {
            if("".equalsIgnoreCase(user.getUserName()))
            {
                holder.im_icon.setImageResource(R.drawable.top_rk_9_u);
            }else{
                holder.im_icon.setImageResource(R.drawable.top_rk_9);
            }
        }else  if(Consts.toprk11.equalsIgnoreCase(app.getTitle()))
        {
            if("".equalsIgnoreCase(user.getUserName()))
            {
                holder.im_icon.setImageResource(R.drawable.top_rk_11_u);
            }else {
                holder.im_icon.setImageResource(R.drawable.top_rk_11);
            }
        }

        holder.rl_item.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(Consts.toprk1.equalsIgnoreCase(app.getTitle()))
                {

                }else  if(Consts.toprk2.equalsIgnoreCase(app.getTitle()))
                {

                    RealTimeListActivity.start(context);
                }else  if(Consts.toprk3.equalsIgnoreCase(app.getTitle()))
                {
                    ConstParaActivity.start(context);
                }else  if(Consts.toprk4.equalsIgnoreCase(app.getTitle()))
                {

                }else  if(Consts.toprk5.equalsIgnoreCase(app.getTitle()))
                {

                }else  if(Consts.toprk6.equalsIgnoreCase(app.getTitle()))
                {

                }else  if(Consts.toprk7.equalsIgnoreCase(app.getTitle()))
                {

                }else  if(Consts.toprk8.equalsIgnoreCase(app.getTitle()))
                {

                }else  if(Consts.toprk9.equalsIgnoreCase(app.getTitle()))
                {

                    if(!"".equalsIgnoreCase(user.getUserName()))
                    {

                    }

                }else  if(Consts.toprk10.equalsIgnoreCase(app.getTitle()))
                {

                    if(!"".equalsIgnoreCase(user.getUserName()))
                    {

                    }
                }else  if(Consts.toprk11.equalsIgnoreCase(app.getTitle()))
                {

                    if(!"".equalsIgnoreCase(user.getUserName()))
                    {

                    }
                }
            }
        });

    }



    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        View view_item;
        ImageView im_icon;
        RelativeLayout rl_item;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            im_icon = (ImageView)itemView.findViewById(R.id.im_icon);
            view_item = (View)itemView.findViewById(R.id.view_item);

            rl_item = (RelativeLayout)itemView.findViewById(R.id.rl_item);
        }
    }

    public void updateData(ArrayList<TopRK> topAppData) {


        this.data.addAll(topAppData);
        notifyDataSetChanged();

    }


}
